package ldk.util.layoutmanagers.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * Created by ldkxingzhe@163.com on 17/3/13.
 */
public abstract class BaseAdapter<VH extends RecyclerView.ViewHolder, T> extends RecyclerView.Adapter<VH> {
    private List<T> mData;

    public interface OnItemClickListener<VH>{
        boolean onItemClick(VH holder);
    }

    private OnItemClickListener<VH> listener;
    public void setOnItemClick(OnItemClickListener<VH> listener){
        this.listener = listener;
    }

    public BaseAdapter(@Nullable List<T> data){
        mData = data;
    }
    public abstract VH onCreateViewHolder(ViewGroup parent, int viewType);


    @Override
    public void onBindViewHolder(final VH holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) listener.onItemClick(holder);
            }
        });
    }

    protected T getItemOfPosition(int position){
        return mData.get(position);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }
}
